from machine import Pin, PWM
import time

# 设置BUZZER引脚和PWM频率
buzzer_pin = Pin(7)
buzzer_pwm = PWM(buzzer_pin)

def play_doorbell():
    # 音符频率
    freqs = [1300, 1000, -1]

    # 音符持续时间
    durations = [333, 333, 333]

    # 播放每个音符
    for i in range(len(freqs)):
        freq = freqs[i]
        duration = durations[i] / 1000.0
        if freqs[i] > 0 :
            buzzer_pwm.freq(freq)
            buzzer_pwm.duty_u16(16384)
        time.sleep(duration)
        buzzer_pwm.duty_u16(0)
        time.sleep(0.1)

# 循环播放门铃声音
while True:
    play_doorbell()
